package edu.yngsxy.communityservice.core.codegen;

import edu.yngsxy.communityservice.core.codegen.ObjectUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/codegen")
@Tag(name = "代码生成器", description = "用于生成entity、dao、service、controller、mapper.xml等代码") // 分组标签
public class CodeGeneratorController {
    private Log log = LogFactory.getLog(CodeGeneratorController.class);
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;

    @PostMapping("/gen")
    @Operation(summary = "生成代码", description = "生成代码功能，需要权限：codegen_gen")
    public String codeGen(@RequestBody @Valid  CodeGeneratorConfig config) {
        CodeGeneratorConfig cfg = CodeGeneratorConfig.create(this.url, this.username, this.password);
        // 复制属性（source -> target）
        ObjectUtil.copyPropertiesIgnoreEmpty(config, cfg);
        CodeGenerator.generate(cfg);//调用代码生成器
        return "代码生成成功,代码生成路径:"+cfg.getProjectPath();
    }
    @GetMapping("/getTableNames")
    @Operation(summary = "数据库所有的表名列表", description = "用于获取数据库的表名列表")
    public java.util.List<String> getTableNames() {
        CodeGeneratorConfig cfg = CodeGeneratorConfig.create(this.url, this.username, this.password);
        return CodeGenerator.getTableNames(cfg);
    }
    @GetMapping("/getTableNamesByPrefix/{prefix}")
    @Operation(summary = "根据表前缀查询数据库的表名列表", description = "用于获取数据库的表名列表")
    public java.util.List<String> getTableNamesByPrefix(@PathVariable @Parameter(description = "表名前缀", required = true) String prefix) {
        CodeGeneratorConfig cfg = CodeGeneratorConfig.create(this.url, this.username, this.password);
        return CodeGenerator.getTableNamesByPrefix(cfg,prefix);
    }

}